﻿<%@ WebHandler Language="C#" Class="DataAjax" %>

using System;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using Aj.Core;

public class DataAjax : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        NameValueCollection dictionarySource =
          (context.Request.RequestType.ToUpper().Equals("GET"))
          ? context.Request.QueryString : context.Request.Form;
        NameValueCollection Nvc = new NameValueCollection(dictionarySource);
        switch (Nvc["act"])
        {
            case "DelPhotoImg": //删除图片
                context.Response.Write(DelPhotoImg(Nvc["Ids"]));
                break;
            case "BatchSavePhotoImgs":  // 保存图片信息
                context.Response.Write(BatchSavePhotoImgs(Nvc["Id"], Nvc["Summary"], Nvc["Sort"]));
                break;
        }
    }

    /// <summary>
    /// 删除图片
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private string DelPhotoImg(string ids)
    {
        if (ids.IsNullOrEmpty())
        {
            return "{\"result\":\"false\",\"prompt\":\"参数不完整(缺少ID)！\"}";
        }
        try
        {
            foreach (string id in ids.Split(","))
            {
                if (id.ToInt() > 0)
                {
                    System.Data.DataSet ds = CustomQuery.PhotoImg_GetPhotoImgById(id.ToInt());
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        ManagedThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DelPhotoImg), new string[] { ds.Tables[0].Rows[0]["BigSrc"].ToString(), ds.Tables[0].Rows[0]["SmallSrc"].ToString() });
                    }
                    int result = CustomQuery.PhotoImg_Delete(id.ToInt());

                    if (result > 0)
                    {
                        return "{\"result\":\"true\",\"prompt\":\"\"}";
                    }
                    else
                    {
                        return "{\"result\":\"false\",\"prompt\":\"操作失败,请重新操作或联系管理员！\"}";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
            return "{\"result\":\"false\",\"prompt\":\"操作失败,处理数据时出错，请重新操作或联系管理员！\"}";
        }
        return "{\"result\":\"false\",\"prompt\":\"操作失败，请重新操作！\"}";
    }

    /// <summary>
    /// 后台删除图片
    /// </summary>
    /// <param name="_imgs"></param>
    private void DelPhotoImg(object _imgs)
    {
        string[] imgs = _imgs as string[];
        if (imgs != null && imgs.Length > 0)
        {
            foreach (string img in imgs)
            {
                if (img.IsNotNullAndEmpty())
                {
                    string _imgPath = Utility.GetMapPath(img);
                    if (File.Exists(_imgPath))
                    {
                        File.Delete(_imgPath);
                    }
                }
            }
        }
    }

    /// <summary>
    /// 保存图片信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private string BatchSavePhotoImgs(string id, string summary, string sort)
    {
        if (id.IsNullOrEmpty() || id.ToInt() <= 0)
        {
            return "{\"result\":\"false\",\"prompt\":\"参数不完整(缺少ID)！\"}";
        }
        try
        {
            int result = CustomQuery.PhotoImg_Update(id.ToInt(), summary, sort.ToInt(0));
            if (result > 0)
            {
                return "{\"result\":\"true\",\"prompt\":\"\"}";
            }
            else
            {
                return "{\"result\":\"false\",\"prompt\":\"操作失败,请重新操作或联系管理员！\"}";
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
            return "{\"result\":\"false\",\"prompt\":\"操作失败,处理数据时出错，请重新操作或联系管理员！\"}";
        }
        return "{\"result\":\"false\",\"prompt\":\"操作失败，请重新操作！\"}";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}